package ru.spb.osll.myfit.client.view.common;

import com.google.gwt.user.client.ui.HTML;
import ru.spb.osll.myfit.client.widget.IStyleConstants;
import ru.spb.osll.myfit.common.TextParsingTools;
import ru.spb.osll.myfit.common.util.Pair;

import java.util.List;

public class TitleFormatter {
  public HTML createSeparatedTitle(String path, String prefix) {
    List<Pair<String, String>> pagePaths = TextParsingTools.getPagePathes(path);

    String htmlString = "";

    if (!pagePaths.isEmpty()) {
      int sizeWithoutLast = pagePaths.size() - 1;
      for (int i = 0; i < sizeWithoutLast; i++){
        Pair<String, String> nameAndPath = pagePaths.get(i);
        htmlString += getHtmlLinkByPair(nameAndPath, prefix) + TextParsingTools.ADDRESS_BAR_SEPARATOR;
      }
      htmlString += pagePaths.get(sizeWithoutLast).getFirst();
    }

    HTML title = new HTML(htmlString);
    title.addStyleName(IStyleConstants.PAGE_TITLE_STYLE_NAME);
    return title;
  }

  public HTML createSeparatedTitle(String path) {
    return createSeparatedTitle(path, "");
  }

  private String getHtmlLinkByPair(Pair<String, String> pageInfo, String prefix){
    return "<a href=\"" + prefix + pageInfo.getSecond() + "\">" + pageInfo.getFirst() + "</a>";
  }
}